/* Copyright (C) 2008 Nick Crafford <nickcrafford@earthlink.net>
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
var image = /[jpg|gif|png|jpeg|JPG|GIF|PNG|JPEG]/;

function complexGameGraphic(peices) {
    this.peices = peices;

    this.collide = function(cgg)
    {
        var hit = false;

        if(cgg instanceof complexGameGraphic)
        {
            for(var i=0; i < peices.length; i++)
                for(var j=0; j < cgg.peices.length; j++)
                    if(peices[i].collide(cgg.peices[j]))
                        hit = true
        }
        else
        {
            alert('The function "collide" requires a complexGameGraphic Object as a parameter');
            return false;
        }

         return hit;
    }

    this.paint = function()
    {

    }

    this.erase = function()
    {

    }

    this.moveX = function(dist)
    {

    }

    this.moveY = function(dist)
    {

    }

    this.xYFlip = function()
    {

    }

    this.xFlip = function()
    {

    }

    this.yFlip = function()
    {

    }
}

function gameGraphic(length, height, left, top, color,flippedDown) {
    var div     = new Object();
    var goXStop = false;
    var goYStop = false;
    var fadeInc = 1;

    this.length                  = length;
    this.height                  = height;
    this.left                    = left;
    this.right                   = left + length;
    this.bottom                  = top  + height;
    this.top                     = top;
    this.color                   = color;
    this.idNumber                = Math.floor(Math.random()*100500000);
    this.rank                    = 0;
    this.callbackFunc            = '';
    this.ondblClickCallBackFunc  = '';
    this.onMouseOverCallBackFunc = '';
    this.enabled                 = true;
    this.north                   = false;
    this.south                   = false;
    this.east                    = false;
    this.west                    = false;
    this.westMoves               = 0;
    this.eastMoves               = 0;
    this.southMoves              = 0;
    this.northMoves              = 0;
    this.type                    = '';
    this.playerNum               = 0;
    this.flippedDown             = flippedDown;
    this.cpu                     = false;

    this.erased = false;

    //The callbackfunction always has the idnumber as its parameter
    this.setClickCallback = function(callbackFunc)
    {
        this.callbackFunc = callbackFunc;
    }

    this.setDblClickCallback = function(ondblClickCallBackFunc)
    {
        this.ondblClickCallBackFunc = ondblClickCallBackFunc;
    }

    this.setMouseOverCallback = function(onMouseOverCallBackFunc)
    {
        this.onMouseOverCallBackFunc = onMouseOverCallBackFunc;
    }

    this.collide = function(gg)
    {
        var hit = false;

        if(gg instanceof gameGraphic)
        {
            for(var a=this.left; a <= this.right; a++)
                for(var b=gg.left; b <= gg.right; b++)
                    if(a == b)
                        inX = true;

            for(var c=this.top; c <= this.bottom; c++)
                for(var d=gg.top; d <= gg.bottom; d++)
                    if(c == d)
                        inY = true;

            if(inY && inX)hit = true;
        }
        else
        {
            alert('The function "collide" requires a gameGraphic Object as a parameter');
            return false;
        }

        return hit;
    }

    this.setRank = function(rank)
    {

        if(rank != 9 && rank != 'B' && rank != 'F')
        {
            this.rank       = rank;
            this.north      = true;
            this.south      = true;
            this.east       = true;
            this.west       = true;
            this.westMoves  = 1;
            this.eastMoves  = 1;
            this.southMoves = 1;
            this.northMoves = 1;
            this.type       = 'piece';
        }
        else if(rank == 9)
        {
            this.rank       = rank;
            this.north      = true;
            this.south      = true;
            this.east       = true;
            this.west       = true;
            this.westMoves  = 10;
            this.eastMoves  = 10;
            this.southMoves = 10;
            this.northMoves = 10;
            this.type       = 'piece';
        }
        else if(rank == 'B')
        {
            this.rank       = rank;
            this.north      = false;
            this.south      = false;
            this.east       = false;
            this.west       = false;
            this.westMoves  = 0;
            this.eastMoves  = 0;
            this.southMoves = 0;
            this.northMoves = 0;
            this.type       = 'piece';
        }
        else if(rank == 'F')
        {
            this.rank       = rank;
            this.north      = false;
            this.south      = false;
            this.east       = false;
            this.west       = false;
            this.westMoves  = 0;
            this.eastMoves  = 0;
            this.southMoves = 0;
            this.northMoves = 0;
            this.type       = 'piece';
        }
    }

    this.paint = function(tColor)
    {
        var tDiv  = window.document.createElement('div');

        var paintColor = '';
        var colorString = '';

        if(tColor != '')
        {
            paintColor = tColor;
        }
        else
        {
            paintColor = this.color;
        }

        if(image.exec(paintColor))
        {
            colorString = 'background-image:url('+paintColor+');';
        }
        else if(paintColor == '')
        {
            colorString = '';
        }
        else
        {
            colorString = 'background-color:'+paintColor+';';
        }

        var styleString = 'position:absolute;'+
                          colorString +
                          'width:'  +this.length + ';' +
                          'height:' +this.height + ';' +
                          'left:'   +this.left   + ';' +
                          'top:'    +this.top    + ';' +
                          'text-align:center;'   +
                          'color:white;'         +
                          'font-size:14px;'      +
                          'font-family:monospace;';

        tDiv.setAttribute('style',styleString);
        tDiv.setAttribute('id',this.idNumber+'');
        if(this.callbackFunc != '') {
            tDiv.setAttribute('onclick', this.callbackFunc+'('+this.idNumber+')');
        }
        tDiv.setAttribute('ondblclick',this.ondblClickCallBackFunc+'('+this.idNumber+')');
        tDiv.setAttribute('onmouseover', this.onMouseOverCallBackFunc+'('+this.idNumber+')');
        document.body.appendChild(tDiv);

        div = tDiv;

        this.erased = false;
    }

    this.erase = function()
    {
        if(!this.erased)document.body.removeChild(div);

        this.erased = true;
    }

    this.moveX = function(dist)
    {
        div.style.left  = (div.offsetLeft + dist) + 'px';

        this.left      += dist;
        this.right     += dist;
    }

    this.goX = function(dist,time)
    {
        var self = this;

        this.moveX(dist);

        if(!goXStop)
            setTimeout(function(){self.goX(dist,time);},time);
    }

    this.goY = function(dist,time)
    {
        var self = this;

        this.moveY(dist);

        if(!goYStop)
            setTimeout(function(){self.goY(dist,time);},time);
    }

    this.changeOpacity = function(amount)
    {
        div.style.MozOpacity=amount;
    }

    this.fadeIn = function(speed)
    {
            var self    = this;

            fadeInc++;

            this.changeOpacity(fadeInc/speed);

            if(fadeInc <= speed)
                setTimeout(function(){self.fadeIn(speed);}, 100);
            else
                fadeInc == 0;
    }

    this.fadeOut = function(speed)
    {
            var self    = this;

            fadeInc--;

            this.changeOpacity(fadeInc/speed);

            if(fadeInc <= speed)
                setTimeout(function(){self.fadeIn(speed);}, 100);
            else
                fadeInc == 0;
    }

    this.changeGraphic = function(newG)
    {
        this.color = newG
        this.erase();
        this.paint();
    }

    this.flipColor = function(newC)
    {
        //var self = this;
        //this.changeGraphic(newC);
        this.erase();
        this.paint(newC);
        //var t = setTimeout(function(){self.changeGraphic(oldC);},timeOut)
    }

    this.moveY = function(dist)
    {
        div.style.top = (div.offsetTop + dist) + 'px';

        this.top      += dist;
        this.bottom   += dist;
    }
}
